package cn.itcast.bos.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.ctc.wstx.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.CourierService;

@ParentPackage("json-default")
@Namespace("/")
@Actions
@Controller
@Scope("protopyte")
public class CourierAction extends ActionSupport implements ModelDriven<Courier>{
	private Courier courier = new Courier();
	@Autowired
	private CourierService  courierService;
	@Override
	public Courier getModel() {
		return courier;
	}
	//添加数据
	@Action(value="courier_save",results={@Result(name="success",location="/pages/base/courier.html",type="redirect")})
	public String CouriterSave(){
		courierService.save(courier);
		return SUCCESS;
	}
	//无条件分页
	private Integer page;
	private Integer rows;
	
	public void setPage(Integer page) {
		this.page = page;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	@Action(value="courier_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//调用一个pageRequest
		Pageable pageable= new PageRequest(page-1,rows);
		//
		Specification<Courier> specification = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = null;
				if(StringUtils.isNoneBlank(courier.getCourierNum())){
					Predicate p1=cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
					predicate=cb.and(p1);
				}
				if(StringUtils.isNoneBlank(courier.getCompany())){
					Predicate p2=cb.equal(root.get("company").as(String.class),courier.getCompany());
					predicate=cb.and(p2);
				}
				if(StringUtils.isNoneBlank(courier.getType())){
					Predicate p3=cb.equal(root.get("type").as(String.class), courier.getType());
					predicate=cb.and(p3);
				}
				//最后一个是多表的
				Join<Courier, Standard> standardJoin = root.join("standard",JoinType.INNER);
				if(courier.getStandard()!=null&&StringUtils.isNoneBlank(courier.getName())){
					Predicate p4= cb.equal(root.get("name").as(String.class),courier.getName());
					predicate = cb.and(p4);
				}
				return predicate;
			}
		};
		
		//
		Page<Courier> pages = courierService.findPageDate(specification,pageable);
		//将数据转成我们需要的json格式
		//利用map集合来转换成我们需要的格式类型
		Map<String, Object> result= new HashMap<String, Object>();
		result.put("total", pages.getTotalElements());
		result.put("rows",pages.getContent());
		//转为json valueStack
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
	//属性驱动
	private String ids;
	
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	//对数据进行删除
	@Action(value="courier_delete",results={@Result(name="success",type="redirect",location="/pages/base/courier.html")})
	public String CourierDel(){
		String[] idArray = ids.split("-");
		//直接交给业务层处理
		courierService.delCourier(idArray);
		return SUCCESS;
	}
	//对数据进行保存
	private String idss;
	public void setIdss(String idss) {
		this.idss = idss;
	}
	@Action(value="courier_savesss",results={@Result(name="success",type="redirect",location="/pages/base/courier.html")})
	public String CourierSave(){
		String[] idsCourier = idss.split("-");
		courierService.courierSave(idsCourier);
		return SUCCESS;
	}
	@Action(value="courier_findassociation",results={@Result(name="success",type="json")})
	//查询我们要的快递员
	public String courier_findassociation(){
		List<Courier> listCourier = courierService.findAll();
		ActionContext.getContext().getValueStack().push(listCourier);
		return SUCCESS;
		
	}
}
